// components/banner/index.js
Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多 slot 支持
  },
  /**
   * 组件的属性列表
   */
  properties: {
    list: Array,
    windowHeight: Number,
    hidden: Boolean
  },

  /**
   * 组件的初始数据
   */
  data: {
    delBtnWidth: 160,
    isScroll: true,
    windowHeight: 0,
  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () { 
      // this._getWindowsHeight()
    },
    moved: function () { },
    detached: function () { },
  }, 
  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show: function () { },
    hide: function () { },
    resize: function () { },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    _getWindowsHeight(){
      var that = this;
      wx.getSystemInfo({
        success: function (res) {
          console.log(res.windowHeight)
          that.setData({
            windowHeight: res.windowHeight
          });
        }
      });
    },
    _drawStart: function (e) {
      // console.log("drawStart");  
      var touch = e.touches[0]

      for (var index in this.data.list) {
        var item = this.data.list[index]
        item.right = 0
      }
      this.setData({
        list: this.data.list,
        startX: touch.clientX,
      })

    },
    test(){

    },
    _drawMove: function (e) {
      var touch = e.touches[0]
      var item = this.data.list[e.currentTarget.dataset.index]
      var disX = this.data.startX - touch.clientX

      if (disX >= 20) {
        if (disX > this.data.delBtnWidth) {
          disX = this.data.delBtnWidth
        }
        item.right = disX
        this.setData({
          isScroll: false,
          list: this.data.list
        })
      } else {
        item.right = 0
        this.setData({
          isScroll: true,
          list: this.data.list
        })
      }
    },
    _drawEnd: function (e) {
      var item = this.data.list[e.currentTarget.dataset.index]
      if (item.right >= this.data.delBtnWidth / 2) {
        item.right = this.data.delBtnWidth
        this.setData({
          isScroll: true,
          list: this.data.list,
        })
      } else {
        item.right = 0
        this.setData({
          isScroll: true,
          list: this.data.list,
        })
      }
    },

    _delItem: function (e) {
      // console.log(e.currentTarget.dataset)
      let index = e.currentTarget.dataset.index
      let id = e.currentTarget.dataset.id
      this.data.list.splice(index, 1)
      this.setData({
        list: this.data.list
      })
      this.triggerEvent('del', id)
      // console.log(this.data.list)
    }
  }
})
